package broker;

import javax.jms.JMSException;

import service.ActiveMQService;
import service.URLService;

import entity.News;

import model.NewsModel;

public class UrlBroker extends Thread {

	public void run() {
		NewsModel newsModel = new NewsModel();
		News news = null;
		int count = 0;
		while (true) {
			news = newsModel.getFalseComplete();
			try {
				if (news == null||count==0) {
					System.out.println("Waiting for get new url !");
					new URLService("http://vnexpress.net/");
					count += 1;
					Thread.currentThread();					
					Thread.sleep(30 * 1000);
				} else {
					ActiveMQService.sendContentSMS(news.getUrl());
					count += 1;
					// calculate to be after one day.
					if (count == 100) {
						new URLService("http://vnexpress.net/");
						count = 0;
					}
					Thread.currentThread();
					Thread.sleep(20 * 1000);
					System.out.println("Sent : " + news.getUrl());
				}
			} catch (JMSException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
